﻿using System;
using System.Collections.Generic;
using System.Text;
using DCRF.Common.Core;
using DCRF.Common.Attributes;
using System.Windows.Forms;
using DCRF.Common.Interface;
using System.Threading;

namespace DCRF.BaseBlocks
{
    [BlockId("FormBuilder")]
    public class FormBuilder: BlockBase
    {
        private Dictionary<string, Form> forms = new Dictionary<string, Form>();

        public FormBuilder(Guid id, IContainerBlockWeb parent)
            : base(id, parent)
        {
        }


        public override void Dispose()
        {
            base.Dispose();

            if (forms.Count > 0)
            {
                foreach (Form frm in forms.Values)
                {
                    frm.Invoke(new MethodInvoker(delegate()
                        {
                            frm.Close();
                        }));
                }
            }

            forms.Clear();
        }

        [BlockService]
        public void BuildForm(string key)
        {
            if (forms.ContainsKey(key)) return;

            Form frm = new Form();
            frm.Tag = key;
            frm.Load += new EventHandler(frm_Load);


            //if we create forms in service methods, owner thread (UI thread) of forms will be caller thread (maybe socket thread)
            //and will be out of our control
            new Thread(new ThreadStart(delegate()
                {
                    Application.Run(frm);
                })).Start();

            forms.Add(key, frm);

            Thread.Sleep(100);
        }

        void frm_Load(object sender, EventArgs e)
        {
            Form frm = sender as Form;

            frm.Text = frm.Tag.ToString();
            frm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            frm.Visible = false;
            frm.MinimizeBox = true;
            frm.MaximizeBox = true;
            frm.ControlBox = true;
        }

        [BlockService]
        public void CloseForm(string key)
        {
            Form frm = forms[key];

            frm.Invoke(new MethodInvoker(delegate()
            {
                frm.Close();
            }));
        }

        [BlockService]
        //this service is not remoteable
        public Control GetForm(string key)
        {
            if (!forms.ContainsKey(key)) Thread.Sleep(900);

            return forms[key];
        }

        [BlockService]
        public void ShowForm(string key)
        {
            if (!forms.ContainsKey(key)) Thread.Sleep(900);

            Form frm = forms[key];

            frm.Invoke(new MethodInvoker(delegate()
                {
                    frm.Show();
                }));
        }

        [BlockService]
        public void HideForm(string key)
        {
            if (!forms.ContainsKey(key)) Thread.Sleep(900);

            Form frm = forms[key];

            frm.Invoke(new MethodInvoker(delegate()
            {
                frm.Hide();
            }));
        }
    }
}
